<?php 
    require_once("include/game_core.php");
    require_once(ADMIN_CONTROLLER_FILE);
    
    if ( ! AdminController::isAdminLogin() ) {
        header( 'Location: index.php' ) ;
    }
    
    //PASS!
    require_once (GAME_TABLE_FILE);
    require_once (GAME_TABLE_FILE);
    
    require_once(VIEW_BASE_FILE);    
    
    $view = new view_base();
    $view->ShowHeader();
    $view->ShowMenu("home");
    $view->ShowConatinerHead();
?>
    <table width="100%">
        <tr>
            <td><h1>Admin Page</h1></td><td>
        </tr>
    </table>
    <h2>Welcome Home Boss!</h2>
    <a href="logout.php">LogOut</a>
    <br/>
    <br/>
<?php

    $result = GameTable::getListOfActiveGames();
    
    if ( $result == "DBERR" ) {
        echo 'The is DB Error, Try again';
    }
    else {
        if ( $result != null)  {
            if ($result->num_rows > 0)  {
                // games exist
                echo '<table width="100%" border ="1">';
                //echo '<caption>Game_ID Player_1 Player_2 StartTime LastTime Moves</caption>';
                echo '<tr><th>Game_ID</th><th>Player_1</th><th>Player_2</th><th>StartTime</th><th>LastTime</th><th>Moves</th><tr>';               
                while($row = $result->fetch_assoc()) {
                    echo '<tr><td>' . $row['id'] . '</td>
                        <td>' . $row['p1_id'] . '</td>
                        <td>' . $row['p2_id'] . '</td>
                        <td>' . date("Y-m-d H:i:s",$row['start_time']) . '</td>
                        <td>' . date("Y-m-d H:i:s",$row['last_time']) . '</td> 
                        <td>' . $row['move_counter'] . '</td></tr>';	
                }
                echo '</table>';
            }
            else {
                echo 'Sorry, No Active Games';
            }
        }
        else {
            return "Sorry, No Active Games ..";
        }        
    }

?>
</div>
<?php   $view->ShowFooter();      ?>